package com.api.sw.duban.servlet;

import com.api.sw.duban.action.DubanTpAction;
import com.api.sw.duban.action.DubanyTzAction;
import com.api.sw.duban.util.ExportWord;
import com.api.sw.duban.util.LogTool;
import com.engine.common.util.ParamUtil;
import com.weaver.formmodel.util.FileHelper;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;
/*
    导出督办事项Word 的servlet/weaver/com.sw.WordOut?action=jhrw&title=计划导出&tzid=7

    /weaver/com.sw.WordOut?action=jcrw&title=12&tzid=8
 */
public class LoadWordServlet extends HttpServlet {
    private final static LogTool log = new LogTool("/log/duban/",false);
    public LoadWordServlet() {
    }
    @Override
    public void doGet(HttpServletRequest var1, HttpServletResponse var2) throws ServletException, IOException {
        Map<String,Object> param=ParamUtil.request2Map(var1);
        if(param!=null){
            log.writeLog("LoadWordServlet--->"+param.toString());
        }
        String action=Util.null2String(param.get("action")).trim() ;
        String filename = Util.null2String(param.get("title")) + ".doc";
        filename = URLEncoder.encode(filename, "UTF-8");
        filename = this.handleSlash(filename);
        filename = filename.replace("%2F", "");
        filename = filename.replaceAll("\\+", "%20");
        var2.setHeader("content-disposition", "attachment; filename=\"" + filename + "\"");
        var2.setContentType("application/x-www-form-urlencoded;charset=ISO-8859-1");
        DubanTpAction dubanTpAction=new DubanTpAction();
        XWPFDocument XWPFDocument=null;
        if("jcrw".equals(action)){//导出任务
            XWPFDocument=dubanTpAction.ExprotWord(param);
        }else if("jhrw".equals(action)){//导出计划
            XWPFDocument=dubanTpAction.ExprotPlayWord(param);
        }else if("jhrwls".equals(action)){//导出计划落实
            XWPFDocument=dubanTpAction.ExprotPlayWordls(param);
        }else if("jhrw".equals(action)){//常规督察导出
            User user = HrmUserVarify.getUser(var1,var2);
            new BaseBean().writeLog("###infoWordOut###       apidatas:  ");

            DubanyTzAction DubantzAction=new DubanyTzAction();
            String sql=DubantzAction.getTablelistSql(ParamUtil.request2Map(var1),user);
            log.writeLog("daochuwdsq1--->"+sql);
            Map<String, Object> datamap=DubantzAction.getWordLsit(sql,DubanyTzAction.pageID,user.getUID()+"");
            int row=Util.getIntValue((Integer) datamap.get("row")+"",1) ;
            int col=Util.getIntValue((Integer)datamap.get("col")+"",1) ;
            ExportWord ew = new ExportWord();
            XWPFDocument = ew.createXWPFDocument(row,col);
        }
        if(XWPFDocument!=null){

            ServletOutputStream var6 = var2.getOutputStream();
            String var7 = FileHelper.getMimeType(filename);
            var2.setContentType(var7);
            XWPFDocument.write(var6);
            var6.flush();
            var6.close();
            XWPFDocument.close();
        }

    }
    private String handleSlash(String var1) {
        return var1.replaceAll("/", "");
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        doGet(req, res);
    }
}
